package com.tianci.appstore.ui.widget;

import java.lang.reflect.Field;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.text.TextPaint;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 自定义textview</br>
 * 功能：描边，跑马灯
 *
 * @author Administrator
 */
public class StrokeTextView extends TextView {

    private TextPaint m_TextPaint;
    private int mainColor = 0;//字体颜色
    private int strokeColor = 0;//描边颜色

    public StrokeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setSingleLine();
        mainColor = Color.WHITE;
        strokeColor = Color.parseColor("#945e00");
    }

    public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSingleLine();
        mainColor = Color.WHITE;
        strokeColor = Color.parseColor("#945e00");
    }

    public StrokeTextView(Context context) {
        super(context);
        setSingleLine();
        mainColor = Color.WHITE;
        strokeColor = Color.parseColor("#945e00");
        setMarqueeRepeatLimit(-1);
        setEllipsize(TruncateAt.MARQUEE);
    }

    /**
     * 设置是否滚动跑马灯
     */
    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
    }

    private boolean m_bDrawSideLine = true; // 默认不采用描边

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.TextView#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        if (m_bDrawSideLine) {
            // 描外层
            // super.setTextColor(Color.BLUE); // 不能直接这么设，如此会导致递归
            m_TextPaint = getPaint();
            setTextColorUseReflection(strokeColor);
            m_TextPaint.setStrokeWidth(5); // 描边宽度
            m_TextPaint.setStyle(Style.FILL_AND_STROKE); // 描边种类
            m_TextPaint.setFakeBoldText(true); // 外层text采用粗体
            m_TextPaint.setShadowLayer(1, 0, 0, 0); // 字体的阴影效果，可以忽略
            super.onDraw(canvas);

            // 描内层，恢复原先的画笔

            // super.setTextColor(Color.BLUE); // 不能直接这么设，如此会导致递归
            setTextColorUseReflection(mainColor);
            m_TextPaint.setStrokeWidth(0);
            m_TextPaint.setStyle(Style.FILL_AND_STROKE);
            m_TextPaint.setFakeBoldText(false);
            m_TextPaint.setShadowLayer(0, 0, 0, 0);
        }
        super.onDraw(canvas);
    }

    /**
     * 反射更改颜色
     *
     * @param color
     */
    private void setTextColorUseReflection(int color) {
        Field textColorField;
        try {
            textColorField = TextView.class.getDeclaredField("mCurTextColor");
            textColorField.setAccessible(true);
            textColorField.set(this, color); //填充具体的color进去
            textColorField.setAccessible(false);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        m_TextPaint.setColor(color);
    }

    /**
     * 设置是否描边，设置完了之后要如果要生效请调用postInvalidate()或者其他还是使之重新绘画显示
     *
     * @param m_bDrawSideLine
     */
    public void setM_bDrawSideLine(boolean m_bDrawSideLine) {
        this.m_bDrawSideLine = m_bDrawSideLine;
        this.postInvalidate();
    }

    /**
     * 设置描边颜色
     *
     * @param color
     */
    public void setStrokeColor(int color) {
        this.strokeColor = color;
    }

    /**
     * 设置字体颜色
     *
     * @param color
     */
    public void setMainColor(int color) {
        this.mainColor = color;
    }
}
